package ch.ethz.fcl.metrobuzz.render.interchange.inter;

import javax.vecmath.Vector2d;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.controller.MBController;
import ch.ethz.fcl.metrobuzz.render.techniques.bezier.Bezier;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.tools.MBTools;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;

public class InterInterchangeTest {
	Point2D c1, c2;
	float radius1, radius2;

	Point2D[] ctrlPoints;

	Point2D[] spline;
	Point2D[] spline2;
	private MBViewer view;
	private MBController control;

	public InterInterchangeTest() {
		init();
	}

	public void run() {
		while (true) {

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			GL11.glPushMatrix();
			view.update();
			render();
			control.update();
			GL11.glPopMatrix();

			Display.update();
		}
	}

	private void init() {
		MBContainer container = new MBContainer();

		int width = 800;
		int height = 600;

		view = new MBViewer(MBViewer.ProjectionType.Perspective, container);
		view.createDisplay(width, height, width, height, this.getClass()
				.getSimpleName());
		view.getCamera().disablePan();
		view.getCamera().initDistance(3.5f);

		control = new MBController(container);

		c1 = new Point2D(-0.9f, 0);
		c2 = new Point2D(0.0f, 0);
		radius1 = 0.2f;
		radius2 = 0.1f;

		Vector2d dir = new Vector2d(c2.getX() - c1.getX(), c2.getY()
				- c1.getY());
		Vector2d verticalDir = MBTools.rotate(dir, -Math.PI / 2);
		float length = c1.distance(c2);
		Point2D midPt = c1.getPointAt(c2, 0.5f);
		Point2D midCtrlPt = midPt.getPointAt(verticalDir, length / 2);
		ctrlPoints = new Point2D[] { c1, midCtrlPt, c2 };

		spline = Bezier.getBezierSpline(100, ctrlPoints);
	}

	private void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glClearColor(1, 1, 1, 1);

		InterInterchangeRender.render(view.getProjection(), c1, radius1, null,
				c2, radius2, null, 0.05f, true);

		// InterInterchangeRender.render(view.getProjection(), c2, radius2, c1,
		// radius1, 0.1f);
	}

	public static void main(String[] arguments) {
		new InterInterchangeTest().run();
	}
}
